var fs = require('fs');

function readCardInfo(path){
	var info = fs.readFileSync(path+'/info_cn.json',{encoding:'UTF-8'});
	//console.log('ReadCardInfo['+path+']: '+info);
	var infoJson = JSON.parse(info);
	console.log('Convert Json obj: '+infoJson);
	return infoJson;
	}

function walk(path){
	//var fileList = new Array();
	var dirList = fs.readdirSync(path);
	var result = [];
 
	/*dirList.forEach(function(item){
		if(fs.statSync(path + '/' + item).isFile()){
			console.log('Find file: '+path + '/' + item);
			fileList.push(path + '/' + item);
		}
	});*/
 
	dirList.forEach(function(item){
		if(fs.statSync(path + '/' + item).isDirectory()){
			console.log('Find card: '+path + '/' + item);
			var info = readCardInfo(path+'/'+item);
			//walk(path + '/' + item);
			console.log('Dir: '+info.number);
			result.push(info);
		}
	});
	console.log('Cards walk result: '+result);
	return result;
}

var cards = {
	all : function(){
		return walk('./cards');
		},
	specify : function(code) {
		return readCardInfo('./cards/' + code);
	}
	}

module.exports = cards;